﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using OpalisConsole.EndUserService;
using OpalisConsole.Views.Administration.Child_Windows;

namespace OpalisConsole.Views.Administration
{
    public partial class UserManagement : Page
    {
        public UserManagement()
        {
               InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (!((bool)App.Current.Resources["FreshStartAdmin"] || (bool)App.Current.Resources["FreshStartApprover"]))
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
            else
            {
                boxFilterType.Items.Add("Machine Name");
                boxFilterType.Items.Add("User Name");
                boxFilterType.Items.Add("Status");

                boxFilterType.SelectedIndex = 0;

                busyIndicatorUserManagement.BusyContent = "Loading User's Information";
                busyIndicatorUserManagement.IsBusy = true;

                EndUserServiceSoapClient proxy = new EndUserServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 30, 0);
                proxy.getAllApprovedRequestsCompleted += new EventHandler<getAllApprovedRequestsCompletedEventArgs>(proxy_getAllApprovedRequestsCompleted);
                proxy.getAllApprovedRequestsAsync();
            }
        }

        void proxy_getAllApprovedRequestsCompleted(object sender, getAllApprovedRequestsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                throw new Exception();
            }
            else
            {
                List<ApprovedComputer> authList = new List<ApprovedComputer>();

                foreach (ApprovedComputer request in e.Result)
                {
                    authList.Add(request);
                }

                if (App.Current.Resources.Contains("ComputerList"))
                {
                    App.Current.Resources.Remove("ComputerList");
                    App.Current.Resources.Add("ComputerList", authList);
                }
                else
                {
                    App.Current.Resources.Add("ComputerList", authList);
                }
                gridUserManagement.ItemsSource = e.Result;
                busyIndicatorUserManagement.IsBusy = false;
            }
        }

        private void boxFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            List<ApprovedComputer> approvalList = (List<ApprovedComputer>)App.Current.Resources["ComputerList"];
            if (boxFilter.Text != String.Empty)
            {
                List<ApprovedComputer> FilterApprovalList = new List<ApprovedComputer>();
                foreach (ApprovedComputer request in approvalList)
                {
                    switch ((String)boxFilterType.SelectedItem)
                    {
                        case "Machine Name":
                            if (request.MachineName.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                        case "User Name":
                            if (request.UserName.ToUpper().Contains(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                        case "Status":
                            if (request.Status.ToUpper().StartsWith(boxFilter.Text.ToUpper()))
                            {
                                FilterApprovalList.Add(request);
                            }
                            break;
                    }
                }
                gridUserManagement.ItemsSource = FilterApprovalList;
            }
            else
            {
                gridUserManagement.ItemsSource = approvalList;
            }
        }
        private void gridUserManagement_GotFocus(object sender, RoutedEventArgs e)
        {
            ApprovedComputer item = (ApprovedComputer)gridUserManagement.SelectedItem;
            AuthorizationRequest request = new AuthorizationRequest()
            {
                ApprovalID = item.ImagingID,
                MachineName = item.MachineName,
                Status = item.Status,
                UserName = item.UserName
            };
            AuthorizationDetailsWindow AuthDeatilsWindow = new AuthorizationDetailsWindow(request);
            AuthDeatilsWindow.Closed += new EventHandler(AuthDeatilsWindow_Closed);
            AuthDeatilsWindow.Show();
        }

        void AuthDeatilsWindow_Closed(object sender, EventArgs e)
        {
            AuthorizationDetailsWindow authWindow = (AuthorizationDetailsWindow)sender;
            if (authWindow.DialogResult.Equals(true))
            {
                List<ApprovedComputer> authList = (List<ApprovedComputer>)App.Current.Resources["ComputerList"];

                int i = 0;
                foreach (ApprovedComputer request in authList)
                {
                    if (request.MachineName.Equals(authWindow.boxMachine.Text) && request.UserName.Equals(authWindow.boxRequester.Text))
                    {
                        request.Status = authWindow.Status;
                        authList.RemoveAt(i);
                        authList.Add(request);
                        if (App.Current.Resources.Contains("ComputerList"))
                        {
                            App.Current.Resources.Remove("ComputerList");
                            App.Current.Resources.Add("ComputerList", authList);
                        }
                        else
                        {
                            App.Current.Resources.Add("ComputerList", authList);
                        }
                        gridUserManagement.ItemsSource = authList;
                        boxFilter.Text = "";
                        break;
                    }
                    i++;
                }
            }
        }
    }
}
